/**
  **************************************************************************
  * @file     main.c
  * @brief    main program
  **************************************************************************
  *                       Copyright notice & Disclaimer
  *
  * The software Board Support Package (BSP) that is made available to
  * download from Artery official website is the copyrighted work of Artery.
  * Artery authorizes customers to use, copy, and distribute the BSP
  * software and its related documentation for the purpose of design and
  * development in conjunction with Artery microcontrollers. Use of the
  * software is governed by this copyright notice and the following disclaimer.
  *
  * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  *
  **************************************************************************
  */


#include "include.h"


/** @addtogroup AT32F403A_periph_examples
  * @{
  */

/** @addtogroup 403A_GPIO_led_toggle GPIO_led_toggle
  * @{
  */

/**
  * @brief  main function.
  * @param  none
  * @retval none
  */
uint32_t start_time;
uint32_t end_time;
int main(void)
{
  nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
  /* jtag-dp disabled and sw-dp enabled */
  gpio_pin_remap_config(SWJTAG_GMUX_010, TRUE);
  
  system_clock_config();
  MeBsp_delay_init();
  MeBsp_Led_Init();
  MeBsp_PWM_Init();
  MeBsp_AD_Init();
  MeBsp_LPF_Init();
  MeBsp_PID_Init();
  MB_Fan_Set_Speed(0);
  
  MeBsp_DevComIn_Init();
  MeBsp_DevComOut_Init();

  MeBSP_ADRC_fhan_Init();

  MeBsp_IBus_Init();
  
  while ( 1 ) {
    if ( MB_Delay.b1ms_flag ) {
      MB_Delay.b1ms_flag = 0;
      start_time = SysTick->VAL;
      MeBsp_Led_Hanl();
      MeBsp_AD_Hanl();
      MeBsp_IBus_Hanl();
      MeBsp_PWM_Hanl();
      MeBsp_DevComIn_Hanl();
      MeBsp_DevComOut_Hanl();
    //  MeBsp_PID_Hanl();
      
      MeBSP_ADRC_fhan_Hanl();
      end_time = SysTick->VAL;
      if ( start_time > end_time ) {
        start_time = start_time - end_time;
      }
      else {
        start_time = (240000-end_time) + start_time;
      }
      
      
     // USER_Debug_Out("PWM time=%d\r\n",start_time);
      
      USER_Debug_Out("IBUS = %d\r\n",MB_AD.wFlt_Val[1]);
    }
    if ( MB_Delay.b100us_flag ) {
      MB_Delay.b100us_flag = 0;
      
      MeBsp_DevComOut_Send();
      MeBsp_DevCom_In_Send();

      MeBsp_IBus_GetOffsetVal();
    }

    
  }
}

/**
  * @}
  */

/**
  * @}
  */
